﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Plat.CRM.Customers.Interface;
using Microhard.Plat.CRM.Customers.DataAccess;
using Microhard.Plat.CRM.Customers.DTO;
using Microhard.Plat.CRM.Customers.Msg;
using Microhard.Plat.CRM.Customers.Models;


namespace Microhard.Plat.CRM.Customers.Impl
{
    public class PrdImpl : ICmpProduct
    {
        public bool IsDuplicate(Models.ProductModel model)
        {
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var cnt = db.Products.Count(n => n.SN == model.SN);
            return cnt > 0 ? true : false;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(Models.ProductModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            //获得当前产品关联的company
            try
            {
                op.CompanyID = this.scope.EmpInfo.CompanyID;
                db.Products.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, ProductMsg.AddFailed);
                Log.Error("IProduct.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.ProductModel model)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var entity = db.Products.SingleOrDefault(l => l.ProductID == model.ProductID);
            if (entity != null)
            {
                entity.Mark = model.Mark;
                //如果产品没有订单，则可以设置为赠品，如果有订单，则无法设置
                if (entity.OrderDetails.Count == 0 && model.IsPromotion)
                {
                    entity.IsPromotion = true;
                    entity.CostCoin = model.CostCoin;
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, ProductMsg.UpdateFailed);
                Log.Error("IProduct.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var entity = db.Products.Where(l => ids.Contains(l.ProductID));
            foreach (var e in entity)
            {
                //是否在使用
                if (e.OrderDetails.Count == 0)
                {
                    db.Products.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(ProductMsg.IsInUsing, e.Mark));
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, ProductMsg.DeleteFailed);
                    Log.Error("IProduct.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.ProductModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ProductModel GetModelByID(Guid id)
        {
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            return db.Products.SingleOrDefault(c => c.ProductID == id).ToModel();
        }

        public Models.ProductModel GetModelByModel(Models.ProductModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ProductModel> Query(Models.ProductModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            IEnumerable<Product> entities = from b in db.Products
                                            where this.scope.PKID.Contains(b.ProductID)
                                            select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderBy(p => p.Mark).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<ProductModel>();
        }

        public List<Models.ProductModel> Query(Models.ProductModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }


        public List<ProductModel> GetPrdsByCompanyID(Guid companyID)
        {
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            return (from p in db.Products
                    where p.CompanyID == companyID
                    select p).OrderBy(p => p.Mark).ToList().Select(t => t.ToModel()).ToList();
        }
    }
}
